import axios from 'axios';

// 你的 TMDb API Key
const API_KEY = '0b7f5e36c645703229cc7988b8bccfcd';

// 令牌
const ACCESS_TOKEN = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIwYjdmNWUzNmM2NDU3MDMyMjljYzc5ODhiOGJjY2ZjZCIsIm5iZiI6MTc1NjkyNDY5NS4wODQsInN1YiI6IjY4Yjg4YjE3NTc1YTFlZmRiNTliYWZhNCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.i7j85IXQg4lwi-OHl8Qqx6vpVt70vghW2OpIWKrOFBE'

const searchMoviesList = async (query: string) => {
  const headers = {
    'Authorization': `Bearer ${ACCESS_TOKEN}`,
    'Accept': 'application/json'
  };

  try {
    const searchUrl = 'https://api.themoviedb.org/3/search/movie';
    console.log(`正在搜索电影: ${query}...`);

    const response = await axios.get(searchUrl, {
      headers,
      params: {
        query: query,
        language: 'zh-CN' // 调整为您需要的语言
      }
    });

    const movies = response.data.results;

    if (!movies || movies.length === 0) {
      console.log(`未找到与 "${query}" 相关的电影。`);
      return;
    }

    console.log(`\n--- 找到 ${movies.length} 部电影 ---`);
    movies.forEach((movie: any, index: number) => {
      console.log(`${index + 1}. 电影名: ${movie.title} (${movie.original_title})`);
      console.log(`   发布日期: ${movie.release_date}`);
      console.log(`   ID: ${movie.id}`);
      console.log('--------------------');
    });

  } catch (error) {
    if (axios.isAxiosError(error)) {
      console.error('API 请求出错:', error.response?.status, error.response?.data);
    } else {
      console.error('发生未知错误:', error);
    }
  }
};
export { searchMoviesList }
